/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.service.property;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.shop.entity.property.HkShopProperty;
import com.thinkgem.jeesite.modules.hk.shop.dao.property.HkShopPropertyDao;
import com.thinkgem.jeesite.modules.hk.shop.entity.property.HkShopPropertyValue;
import com.thinkgem.jeesite.modules.hk.shop.dao.property.HkShopPropertyValueDao;

/**
 * 乐购属性Service
 * @author 罗天文
 * @version 2018-05-15
 */
@Service
@Transactional(readOnly = true)
public class HkShopPropertyService extends CrudService<HkShopPropertyDao, HkShopProperty> {

	@Autowired
	private HkShopPropertyValueDao hkShopPropertyValueDao;
	
	public HkShopProperty get(String id) {
		HkShopProperty hkShopProperty = super.get(id);
		hkShopProperty.setHkShopPropertyValueList(hkShopPropertyValueDao.findList(new HkShopPropertyValue(hkShopProperty)));
		return hkShopProperty;
	}
	
	public List<HkShopProperty> findList(HkShopProperty hkShopProperty) {
		return super.findList(hkShopProperty);
	}
	
	public Page<HkShopProperty> findPage(Page<HkShopProperty> page, HkShopProperty hkShopProperty) {
		return super.findPage(page, hkShopProperty);
	}
	
	@Transactional(readOnly = false)
	public void save(HkShopProperty hkShopProperty) {
		super.save(hkShopProperty);
		for (HkShopPropertyValue hkShopPropertyValue : hkShopProperty.getHkShopPropertyValueList()){
			if (hkShopPropertyValue.getId() == null){
				continue;
			}
			if (HkShopPropertyValue.DEL_FLAG_NORMAL.equals(hkShopPropertyValue.getDelFlag())){
				if (StringUtils.isBlank(hkShopPropertyValue.getId())){
					hkShopPropertyValue.setProperty(hkShopProperty);
					hkShopPropertyValue.preInsert();
					hkShopPropertyValueDao.insert(hkShopPropertyValue);
				}else{
					hkShopPropertyValue.preUpdate();
					hkShopPropertyValueDao.update(hkShopPropertyValue);
				}
			}else{
				hkShopPropertyValueDao.delete(hkShopPropertyValue);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(HkShopProperty hkShopProperty) {
		super.delete(hkShopProperty);
		hkShopPropertyValueDao.delete(new HkShopPropertyValue(hkShopProperty));
	}
	
}